<!DOCTYPE html>
<html lang="en">
<head>
<link href="js/kindeditor-4.1.10/themes/default/default.css" type="text/css" rel="stylesheet">
    <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width"/>
        <link href="js/kindeditor-4.1.10/themes/default/default.css" type="text/css" rel="stylesheet">
        <script type="text/javascript" charset="utf-8" src="js/kindeditor-4.1.10/kindeditor-all-min.js"></script>
        <script type="text/javascript" charset="utf-8" src="js/kindeditor-4.1.10/lang/zh_CN.js"></script>
    <script type="text/javascript" src="js/cookie.js" charset="utf-8"></script>
        <title>Document</title>
        <link rel="stylesheet" href="css/reset.css" type="text/css"/>
        <link rel="stylesheet" href="css/gonggong.css" type="text/css"/>
        <link rel="stylesheet" href="css/shoppingMall.css" type="text/css"/>
        <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.4.1/themes/default/easyui.css" />
        <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.4.1/themes/icon.css" />
</head>

<body>
<div style="padding:10px 10px 10px 10px">
    <form id="itemAddForm" class="itemForm" method="post">
        <table cellpadding="5">
            <tr>
                <td>房源分类:</td>
                <td>
                    <input type="text" name="houseType" style="width: 280px;"></input>
                </td>
            </tr>
            <tr>
                <td>房源标题:</td>
                <td><input class="easyui-textbox" type="text" name="houseName" data-options="required:true" style="width: 280px;"></input></td>
            </tr>
            <tr>
                <td>房产价格:</td>
                <td><input class="easyui-numberbox" type="text" name="productPrice" data-options="min:1,max:99999999,precision:2,required:true" />
                </td>
            </tr>
            <tr>
                <td>房产面积:</td>
                <td><input class="easyui-numberbox" type="text" name="houseArea" data-options="min:1,max:99999999,precision:0,required:true" /></td>
            </tr>
			<tr>
                <td>省市级位置:</td>
                <td><input class="easyui-textbox" type="text" name="addrProvince" data-options="required:true" style="width: 280px;"></input></td>
            </tr>
			<tr>
                <td>区县级位置:</td>
                <td><input class="easyui-textbox" type="text" name="addrDistrict" data-options="required:true" style="width: 280px;"></input></td>
            </tr>
            <tr>
                <td>房源图片:</td>
                <td>
                    <input id="imgInput" type="text" name="imgUrl" value=""/>
                </td>
            </tr>
			<tr>
                <td>房源详细地址:</td>
                <td>
                    <textarea style="width:800px;height:200px;" name="houseAddr"></textarea>
                </td>
            </tr>
            <tr>
                <td>房源描述:</td>
                <td>
                    <textarea style="width:800px;height:200px;" name="houseDescription"></textarea>
                </td>
            </tr>
			<tr >
                <td>拍卖/销售时间:</td>
                <td>
                    <input id="startTime" type="text" name="startTime" value=""/>格式"YYYY-MM-DD hh:mm:ss"
                </td>
            </tr>
			<tr hidden>
                <td>用户名:</td>
                <td>
                    <input id="userId" type="text" name="userId" value=""/>
                </td>
            </tr>
        </table>
    </form>
    <span><i></i>选择商品图片：</span>
    <img id="photo" src="img/shangchuanBg.png" width="109px" height="109px" style="display: block;"/>
    <form id="form1" action="" method="post" enctype="multipart/form-data">
        <input  type="file" name="imgOne" class="imgOne" id="imgOne"
                onchange=""/>
    </form>
    <div style="padding:5px">
        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()">提交</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()">重置</a>
    </div>
</div>
</div>
<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/shoppingMall.js"></script>
<script src="js/shoppingDetail.js"></script>
<script>
	 $(function(){
            // alert("好啊1");
            var userId="";
            var _ticket = getCookie("EM_TICKET");
            if(!_ticket){
                window.location.href="login.html";
                return ;
            }
            //当dataType类型为jsonp时，jQuery就会自动在请求链接上增加一个callback的参数
            $.ajax({
                url : "http://www.easyhouse.com/user/query/" + _ticket,
                dataType : "json",
                type : "GET",
                success : function(data){

                    if(data.status == 200){
                        var _data = JSON.parse(data.data);//jackson
						//console.log(_data);
                        if(_data==null){
                            alert("啥也别说了，你的用户超时了,赶紧查查redis还有数据没有");
                            window.location.href="./login.html";
                            return;}
                        /* var html =_data.userName+"，欢迎来到好房！<a href=\"javascript:void(0)\" class=\"link-logout\" onclick='logout()'>[退出]</a>  \|<a href=\"http://www.easymall.com/manage.html\" class=\"link-logout\">[后台]</a>";
                        userId=_data.userId;
                        
                        query(userId); */
						var element = document.getElementById("userId");
						element.setAttribute("value",_data.userName);
						//console.log($("#userName"));
                    }else{
                        alert("咋了兄弟，购物车没东西自己心里没数吗，赶紧购物去");
                    }
                },
                error : function(){
                    alert('index error.');
                }
            });
        });
    function clearForm(){
        //alert("1111");
       document.getElementById("itemAddForm").reset();
        document.getElementById("photo").src="img/shangchuanBg.png";
    }
    function submitForm(){
       //alert("2222");
        alert($("#itemAddForm").serialize());
        $.post("/products/save",$("#itemAddForm").serialize(), function(data){
            if(data.status == 200){
                alert("新增房源成功");
            }
        });
    }
    /* 图片大小判定 */
    function validate(a) {
        var file = a.value;
        if (!/.(gif|jpg|jpeg|png|GIF|JPG|png)$/.test(file)) {
            alert("图片类型必须是.gif,jpeg,jpg,png中的一种");
            return;
        } else {
            var image = new Image();
            image.src = file;
            var height = image.height;
            var width = image.width;
            var filesize = image.filesize;
            /*alert(height+"x.."+width);*/
            if (width != 300 && height != 300 && filesize > 32768) {
                alert('请上传300*300像素 或者大小32k的图片');
                return;
            }
        }


    }

    /* 本地上传 */
    function getFileUrl(sourceId) {
        var url;
        if (navigator.userAgent.indexOf("MSIE") >= 1) { // IE
            url = document.getElementById(sourceId).value;
        } else if (navigator.userAgent.indexOf("Firefox") > 0) { // Firefox
            url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0));
        } else if (navigator.userAgent.indexOf("Chrome") > 0) { // Chrome
            url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0));
        }
        return url;
    }

    function preImg(sourceId, targetId) {
        var url = getFileUrl(sourceId);
        var imgPre = document.getElementById(targetId);
        imgPre.src = url;
    }

    $("#imgOne").change(function () {
        var formData = new FormData();
        formData.append("pic", document.getElementById("imgOne").files[0]);
        $.ajax({
            type: "POST", // 数据提交类型
            url: "http://www.easyhouse.com/uploadImg", // 发送地址
            data: formData, //发送数据
            async: true, // 是否异步
            processData: false, //processData 默认为false，当设置为true的时候,jquery ajax 提交的时候不会序列化 data，而是直接使用data
            contentType: false, //
            success:function (data) {
                if(data.error==0){
                    //alert(data.error);
                    alert(data.url);
                    document.getElementById("imgInput").value=data.url;
                    document.getElementById("photo").src=data.url;
                }
            }
        });
    });


</script>
</body>
</html>